package com.aop.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * CGLIB的代理类
 */
public class CGLIBProxy {
    private Object target;

    public Object createCGLIBProxy(Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(new CallBackClass());
        return enhancer.create();
    }

    private class CallBackClass implements InvocationHandler {

        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            System.out.println(o.getClass());
            System.out.println(method.getName());
            if (objects!=null) {
                System.out.println(Arrays.toString(objects));
            }
            return method.invoke(target, objects);
        }
    }
}
